package net.begincode.biz.impl;

import net.begincode.bean.UserBaseInfo;
import net.begincode.biz.UserBiz;
import net.begincode.blog.bean.param.UserQueryParam;
import net.begincode.blog.entity.User;
import net.begincode.blog.service.UserService;
import net.begincode.common.Encoder;
import net.begincode.convert.UserConvertUtils;
import net.begincode.page.PageResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class UserBizImpl implements UserBiz {
    private final static String SALT = "begincode";
    private static final Logger LOGGER = LoggerFactory.getLogger(UserBizImpl.class);
    @Resource
    private UserService userService;
    @Resource
    private Encoder encoder;

    @Override
    public User checkUser(String email, String pwd) {
        User user = userService.queryUserByEmail(email);
        if (user != null && encoder.encodeMD5(pwd, SALT).equals(user.getPwd())) {
            //清空关键数据
            user.setPwd("");
            user.setPhone("");
            user.setOpenId("");
            user.setAccessToken("");
            return user;
        }
        LOGGER.error("{}用户或密码错误", email);
        return null;
    }

    @Override
    public PageResponse<User> query(UserQueryParam userQueryParam) {
        return userService.selectByParam(userQueryParam);
    }

    @Override
    public Boolean updateUserFlag(Long userId, String flag) {
        return userService.updateUserFlag(userId, flag) > 0;
    }

    @Override
    public List<UserBaseInfo> queryActiveUser(int limit) {
        List<User> users = userService.queryActiveUser(limit);
        return UserConvertUtils.convert(users);
    }
}
